home *** CD-ROM | disk | FTP | other *** search
- import java.applet.Applet;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Dimension;
- import java.awt.Event;
- import java.awt.Font;
- import java.awt.Graphics;
-
- public class SpreadSheet extends Applet {
- String title;
- Font titleFont;
- Color cellColor;
- Color inputColor;
- int cellWidth = 100;
- int cellHeight = 15;
- int titleHeight = 15;
- int rowLabelWidth = 15;
- Font inputFont;
- boolean isStopped = false;
- boolean fullUpdate = true;
- int rows;
- int columns;
- int currentKey = -1;
- int selectedRow = -1;
- int selectedColumn = -1;
- SpreadSheetInput inputArea;
- Cell[][] cells;
- Cell current;
-
- public synchronized void init() {
- this.cellColor = Color.white;
- this.inputColor = new Color(100, 100, 225);
- this.inputFont = new Font("Courier", 0, 10);
- this.titleFont = new Font("Courier", 1, 12);
- this.title = ((Applet)this).getParameter("title");
- if (this.title == null) {
- this.title = "Spreadsheet";
- }
-
- String rs = ((Applet)this).getParameter("rows");
- if (rs == null) {
- this.rows = 9;
- } else {
- this.rows = Integer.parseInt(rs);
- }
-
- rs = ((Applet)this).getParameter("columns");
- if (rs == null) {
- this.columns = 5;
- } else {
- this.columns = Integer.parseInt(rs);
- }
-
- this.cells = new Cell[this.rows][this.columns];
- char[] l = new char[1];
-
- for(int i = 0; i < this.rows; ++i) {
- for(int j = 0; j < this.columns; ++j) {
- this.cells[i][j] = new Cell(this, Color.lightGray, Color.black, this.cellColor, this.cellWidth - 2, this.cellHeight - 2);
- l[0] = (char)(97 + j);
- rs = ((Applet)this).getParameter("" + new String(l) + (i + 1));
- if (rs != null) {
- this.cells[i][j].setUnparsedValue(rs);
- }
- }
- }
-
- Dimension d = ((Component)this).size();
- this.inputArea = new SpreadSheetInput((String)null, this, d.width - 2, this.cellHeight - 1, this.inputColor, Color.white);
- ((Component)this).resize(this.columns * this.cellWidth + this.rowLabelWidth, (this.rows + 1) * this.cellHeight + this.cellHeight + this.titleHeight);
- }
-
- public void setCurrentValue(float val) {
- if (this.selectedRow != -1 && this.selectedColumn != -1) {
- this.cells[this.selectedRow][this.selectedColumn].setValue(val);
- ((Component)this).repaint();
- }
- }
-
- public void stop() {
- this.isStopped = true;
- }
-
- public void start() {
- this.isStopped = false;
- }
-
- public void destroy() {
- for(int i = 0; i < this.rows; ++i) {
- for(int j = 0; j < this.columns; ++j) {
- if (this.cells[i][j].type == 2) {
- this.cells[i][j].updaterThread.stop();
- }
- }
- }
-
- }
-
- public void setCurrentValue(int type, String val) {
- if (this.selectedRow != -1 && this.selectedColumn != -1) {
- this.cells[this.selectedRow][this.selectedColumn].setValue(type, val);
- ((Component)this).repaint();
- }
- }
-
- public void update(Graphics g) {
- if (this.fullUpdate) {
- this.paint(g);
- this.fullUpdate = false;
- } else {
- g.setFont(this.titleFont);
-
- for(int i = 0; i < this.rows; ++i) {
- for(int j = 0; j < this.columns; ++j) {
- if (this.cells[i][j].needRedisplay) {
- int cx = j * this.cellWidth + 2 + this.rowLabelWidth;
- int cy = (i + 1) * this.cellHeight + 2 + this.titleHeight;
- this.cells[i][j].paint(g, cx, cy);
- }
- }
- }
-
- }
- }
-
- public void recalculate() {
- for(int i = 0; i < this.rows; ++i) {
- for(int j = 0; j < this.columns; ++j) {
- if (this.cells[i][j] != null && this.cells[i][j].type == 3) {
- this.cells[i][j].setRawValue(this.evaluateFormula(this.cells[i][j].parseRoot));
- this.cells[i][j].needRedisplay = true;
- }
- }
- }
-
- ((Component)this).repaint();
- }
-
- public float evaluateFormula(Node n) {
- float val = 0.0F;
- if (n == null) {
- return val;
- } else {
- switch (n.type) {
- case 0:
- val = this.evaluateFormula(n.left);
- switch (n.op) {
- case '*':
- val *= this.evaluateFormula(n.right);
- return val;
- case '+':
- val += this.evaluateFormula(n.right);
- return val;
- case ',':
- case '.':
- default:
- return val;
- case '-':
- val -= this.evaluateFormula(n.right);
- return val;
- case '/':
- val /= this.evaluateFormula(n.right);
- return val;
- }
- case 1:
- return n.value;
- case 2:
- if (n != null && this.cells[n.row][n.column] != null) {
- return this.cells[n.row][n.column].value;
- }
- }
-
- return val;
- }
- }
-
- public synchronized void paint(Graphics g) {
- char[] l = new char[1];
- Dimension d = ((Component)this).size();
- g.setFont(this.titleFont);
- int i = g.getFontMetrics().stringWidth(this.title);
- g.drawString(this.title == null ? "Spreadsheet" : this.title, (d.width - i) / 2, 12);
- g.setColor(this.inputColor);
- g.fillRect(0, this.cellHeight, d.width, this.cellHeight);
- g.setFont(this.titleFont);
-
- for(int i = 0; i < this.rows + 1; ++i) {
- int cy = (i + 2) * this.cellHeight;
- g.setColor(((Component)this).getBackground());
- g.draw3DRect(0, cy, d.width, 2, true);
- if (i < this.rows) {
- g.setColor(Color.red);
- g.drawString("" + (i + 1), 2, cy + 12);
- }
- }
-
- g.setColor(Color.red);
-
- for(int i = 0; i < this.columns; ++i) {
- int cx = i * this.cellWidth;
- g.setColor(((Component)this).getBackground());
- g.draw3DRect(cx + this.rowLabelWidth, 2 * this.cellHeight, 1, d.height, true);
- if (i < this.columns) {
- g.setColor(Color.red);
- l[0] = (char)(65 + i);
- g.drawString(new String(l), cx + this.rowLabelWidth + this.cellWidth / 2, d.height - 3);
- }
- }
-
- for(int i = 0; i < this.rows; ++i) {
- for(int j = 0; j < this.columns; ++j) {
- int cx = j * this.cellWidth + 2 + this.rowLabelWidth;
- int cy = (i + 1) * this.cellHeight + 2 + this.titleHeight;
- if (this.cells[i][j] != null) {
- this.cells[i][j].paint(g, cx, cy);
- }
- }
- }
-
- g.setColor(((Component)this).getBackground());
- g.draw3DRect(0, this.titleHeight, d.width, d.height - this.titleHeight, false);
- this.inputArea.paint(g, 1, this.titleHeight + 1);
- }
-
- public boolean mouseDown(Event evt, int x, int y) {
- if (y < this.titleHeight + this.cellHeight) {
- this.selectedRow = -1;
- if (y <= this.titleHeight && this.current != null) {
- this.current.deselect();
- this.current = null;
- }
-
- return true;
- } else if (x < this.rowLabelWidth) {
- this.selectedRow = -1;
- if (this.current != null) {
- this.current.deselect();
- this.current = null;
- }
-
- return true;
- } else {
- this.selectedRow = (y - this.cellHeight - this.titleHeight) / this.cellHeight;
- this.selectedColumn = (x - this.rowLabelWidth) / this.cellWidth;
- if (this.selectedRow <= this.rows && this.selectedColumn < this.columns) {
- if (this.selectedRow >= this.rows) {
- this.selectedRow = -1;
- if (this.current != null) {
- this.current.deselect();
- this.current = null;
- }
-
- return true;
- }
-
- Cell cell = this.cells[this.selectedRow][this.selectedColumn];
- this.inputArea.setText(new String(cell.getPrintString()));
- if (this.current != null) {
- this.current.deselect();
- }
-
- this.current = cell;
- this.current.select();
- ((Component)this).requestFocus();
- this.fullUpdate = true;
- ((Component)this).repaint();
- } else {
- this.selectedRow = -1;
- if (this.current != null) {
- this.current.deselect();
- this.current = null;
- }
- }
-
- return true;
- }
- }
-
- public boolean keyDown(Event evt, int key) {
- this.fullUpdate = true;
- this.inputArea.keyDown(key);
- return true;
- }
- }
-